package com.example.demo4;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@WebServlet("/download")
public class Download extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathPrefix = "/Users/liguosheng/Desktop/Kits/apache-tomcat-8.5.71/bin";
        String path = pathPrefix + req.getParameter("p").replace("'","");
//        System.out.println(new File("test.txt").getAbsolutePath());
        String filename = Upload.FILE_NAME;
        System.out.println(filename + "        " + path);
        //设置响应的头信息和响应的类型
        resp.setContentType("application/octet-stream");
        resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        //跳转页面
//        req.getRequestDispatcher(path).forward(req, resp);
        //清空缓存区
        resp.flushBuffer();
    }
}
